package vaani.internal.purple;

import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author sourcemorph
 */

public class BuddyLocator {
    /*class containing independent logic of searching through buddy list, again the approach will be
     * to have multiple passes, and with each subsequent pass, relaxing the search criterion*/
    private List<Result> queryResult;
    private List<String> arguments;

    public BuddyLocator(List<String> arguments) {
        this.arguments = arguments;
    }

    public List<Result> getResults() {
        /*pass one: with all arguments*/
        for(String a : arguments) {
            System.out.println("argument:" + a);
        }
        //BuddyListHandler.readFile();
        queryResult = BuddyListHandler.findBuddies(arguments);
        for(Result r : queryResult) {
            System.out.println("result: " + r.getBuddy().getName());
        }
        if(queryResult.size() != 0) {
            System.out.println("77");
            return queryResult;
        }
        /*pass two, maybe one of the arguments was the protocol name, consider that and retry*/
        for(String s : arguments) {
            if(BuddyListHandler.matchProtocol(s)) {
                List<String> tempQuery = new LinkedList<String>(arguments);
                tempQuery.remove(s);
                queryResult = BuddyListHandler.findBuddies(tempQuery);
                if(queryResult.size() != 0) {
                    return queryResult;
                }
            }
        }
        return null;
    }

    /*trial
     public static void main(String [] args) {
        Vector<String> temp = new Vector<String>();
        temp.add("nihar");
        Vector<Result> r = new BuddyLocator(temp).getResults();
        for(Result i : r) {
            System.out.println(i.getBuddy().getName());
        }
    }*/

    /*trial2
     public static void main(String [] args) {
        Vector<String> temp = new Vector<String>();
        temp.add("nihar");
        Vector<Result> tempResult = BuddyListHandler.findBuddies(temp);
        for(Result r : tempResult) {
            System.out.println(r.getBuddy().getName());
        }
    }*/
}
